#include "TerrainObject.h"
#include "Transformation.h"
#include "Terrain.h"
#include "AudioSource.h"
#include "AudioControl.h"

using namespace game_engine;
using namespace game_engine::library;
using namespace game_engine::components;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

TerrainObject::TerrainObject(void)
    : GameObject()
{
}

TerrainObject::TerrainObject(Scene *scn, const string &n, const string &t, 
    const DrawLevelType d)
    : GameObject(scn, n, t, d)
{
    // Create its components
    Transformation *worldTransf = new Transformation(this, 0.0, 0.0, 0.0, 0.0, 
        1.0, 1.0);
    Terrain *terr = new Terrain(this, "", "");
    AudioSource *bgm = new AudioSource(this, "bgm", MUSIC);
    AudioControl *audioControl = new AudioControl(this);

    // Add components to object
    addComponent(worldTransf);
    addComponent(terr);
    addComponent(bgm);
    addComponent(audioControl);
}

TerrainObject::~TerrainObject(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void TerrainObject::playBgm(void) throw(...)
{
    // Get the AudioSource component
    AudioSource *audio = dynamic_cast<AudioSource*> (getComponent("AudioSource"));
    audio->play();
}

void TerrainObject::muteBgm(const bool mute) throw (...)
{
    // Get the AudioSource component
    AudioSource *audio = dynamic_cast<AudioSource*> (getComponent("AudioSource"));
    audio->setMute(mute);
}

void TerrainObject::stopBgm(void) throw(...)
{
    // Get the AudioSource component
    AudioSource *audio = dynamic_cast<AudioSource*> (getComponent("AudioSource"));
    audio->stop();
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void TerrainObject::setDescriptors(const string &infoFile, 
                                   const string &textFile)
{
    // Get the Terrain component
    Terrain *terr = dynamic_cast<Terrain*> (getComponent("Terrain"));
    terr->setTileInfoFile(infoFile);
    terr->setTileTextureFile(textFile);
}

void TerrainObject::setBgm(const string &audioFile, const bool loop)
{
    // Get the AudioSource component
    AudioSource *audio = dynamic_cast<AudioSource*> (getComponent("AudioSource"));
    audio->setAudioFile(audioFile);
    audio->setLoop(loop);
}
